/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package TestCase.InvokeInterface;

import classgenerator.logic.FieldType;
import classgenerator.logic.Oracle;
import classgenerator.logic.OracleImpl;
import deadCodeRmv.StartMeDCR;
import java.util.ArrayList;
import takatuka.classreader.dataObjs.ClassFile;
import takatuka.classreader.dataObjs.MethodInfo;
import takatuka.classreader.dataObjs.Un;
import takatuka.classreader.logic.constants.FieldTypes;
import takatuka.classreader.logic.constants.JavaInstructionsOpcodes;
import takatuka.classreader.logic.factory.FactoryFacade;
import takatuka.classreader.logic.factory.FactoryPlaceholder;

/**
 *
 * @author irfan
 */
public class TestSimpleInterface extends StartMeDCR {

    public static void main(String args[]) throws Exception {
        new TestSimpleInterface().start(args);
    }

    @Override
    public void execute(String args[]) throws Exception {
        Oracle oracle = new OracleImpl();
        FactoryFacade factoryFacade = FactoryPlaceholder.getInstanceOf().getFactory();
        ClassFile cFile = oracle.createEmptyClassFile("TestSimpleInterface", "TestSimpleInterface.java");



        MethodInfo constrMethodInfo = oracle.addDefaultConstructor(cFile);


        int defaultConstrRefInfo = oracle.addMethodRefInCP(cFile, cFile.getThisClass().intValueSigned(),
                oracle.getUTF8FromCP(cFile, constrMethodInfo.getNameIndex().intValueSigned()),
                oracle.getUTF8FromCP(cFile, constrMethodInfo.getDescriptorIndex().intValueSigned()));

        int classInfo = oracle.addClassInfoInCP(cFile, oracle.addUTF8InCP(cFile, "ClassImpleInterface"));



        ArrayList<FieldType> parm = new ArrayList<FieldType>();
        parm.add(new FieldType(FieldTypes.BOOLEAN));
        //  parm.add(new FieldType(FieldTypes.TYPE_JINT));
        FieldType ret = new FieldType(FieldTypes.VOID);

        int cCtor = oracle.addMethodRefInCP(cFile, classInfo, "<init>", "()V");
        int InterfaceInfoIndex = oracle.addClassInfoInCP(cFile, oracle.addUTF8InCP(cFile, "Interface"));
        int myMethodIndex = oracle.addInterfaceMethodRefInCP(cFile, InterfaceInfoIndex, "my", "()V");

        /**
         * added the main method in the class file without any code
         */
        MethodInfo mainMethod = oracle.addMainFunction(cFile);
        oracle.createObj(cFile, mainMethod, classInfo,
                cCtor);
        /**
         * ASTORE_1
         */
        oracle.addInstruction(cFile, mainMethod, factoryFacade.createInstruction(JavaInstructionsOpcodes.ASTORE_1, mainMethod.getCodeAtt()));
        /*
         * Aload_1
         * 
         */
        oracle.addInstruction(cFile, mainMethod, factoryFacade.createInstruction(JavaInstructionsOpcodes.ALOAD_1, mainMethod.getCodeAtt()));
        // oracle.addMethodRefInCP(cFile, classInfo, "my", "()V");

        /**
         * INVOKEINTERFACE
         *
         */
        Un operand = factoryFacade.createUn(myMethodIndex).trim(2);
        operand.conCat(factoryFacade.createUn(0x0100).trim(2));

        oracle.addInstruction(cFile, mainMethod, factoryFacade.createInstruction(JavaInstructionsOpcodes.INVOKEINTERFACE,
                operand, mainMethod.getCodeAtt()));




        /**
         * return
         */
        oracle.addInstruction(cFile, mainMethod, factoryFacade.createInstruction(JavaInstructionsOpcodes.RETURN, mainMethod.getCodeAtt()));
        super.execute(args);

    }
}
